﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using DbMapping.Common;
using VideoCache.Server.DAL;
using VideoCache.Server.Model;
using VideoCache.Aggregation;

namespace VideoCache.Server.BLL
{
    public partial class ChannelUrlBLL
    {
        public static ChannelUrlInfo GetTokenByUrl(string url)
        {
            Uri uri;
            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                throw new ArgumentException("错误的网址格式：" + url);
            }

            var channels = SiteChannelBLL.FindAll(null, null);

            foreach (var channel in channels)
            {
                if (uri.Host.ToLower() == channel.Domain)
                {
                    return GetVideoFromChannel(url, channel);
                }
            }

            throw new NotImplementedException("不支持的网址：" + url);
        }

        private static ChannelUrlInfo GetVideoFromChannel(string url, SiteChannelInfo channel)
        {
            Match match = Regex.Match(url, channel.UrlPattern);

            if (match.Success)
            {
                Conditions cons = new Conditions();
                cons.Add(new ConditionItem(LinkType.And, ChannelUrlInfo.UrlField, CompareType.Equal, url));

                ChannelUrlInfo info = ChannelUrlDAL.Find(cons, null);

                if (info == null)
                {
                    info = new ChannelUrlInfo();
                    info.Url = url;
                    info.ChannelId = channel.Id;

                    info.Id = ChannelUrlDAL.Add(info);
                }

                if (info.TokenId == 0)
                {
                    VideoModel model = VideoCache.Aggregation.FetcherFactory.GetToken(url);

                    if (model == null) throw new Exception("无法从指定的网址获取标记：" + url);

                    Conditions consToken = new Conditions();
                    consToken.Add(new ConditionItem(LinkType.And, ChannelTokenInfo.ChannelIdField, CompareType.Equal, channel.Id));
                    consToken.Add(new ConditionItem(LinkType.And, ChannelTokenInfo.TokenField, CompareType.Equal, model.Token));
                    ChannelTokenInfo channelToken = ChannelTokenDAL.Find(consToken, null);

                    if (channelToken == null)
                    {
                        channelToken = new ChannelTokenInfo();
                        channelToken.Token = model.Token;
                        channelToken.VideoName = model.VideoName;
                        channelToken.ChannelId = channel.Id;
                        channelToken.RequestTimes = 0;
                        channelToken.Id = ChannelTokenDAL.Add(channelToken);
                    }

                    info.TokenId = channelToken.Id;
                    ChannelUrlDAL.Update(info);
                }

                return info;
            }

            throw new ArgumentException("网址不匹配：" + url);
        }

        public static SiteInfo FetcherUrl(string url)
        {
            string host = Fsb.Utility.UrlHelper.GetHost(url);

            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, SiteInfo.UrlHostField, CompareType.Equal, host));

            return SiteDAL.Find(cons, null);
        }
    }
}